﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Oracle.DataAccess.Client;
using Oracle.DataAccess.Types;  
namespace ITXTEST
{
    public partial class ITXTESTData
    {
        public ITXTESTData()
        { }
        public void OracleENMQ()
        {
            string _connstring = "Data Source=intex1;User Id=intex;Password=intex;";
            OracleConnection _connObj = new OracleConnection(_connstring);

            // Create a new queue object
            OracleAQQueue _queueObj = new OracleAQQueue("MESSAGE_QUEUE", _connObj);
            _connObj.Open();

            OracleTransaction _txn = _connObj.BeginTransaction();

            // Set the payload type to your UDT
            _queueObj.MessageType = OracleAQMessageType.Raw;
            OracleAQMessage _msg = new OracleAQMessage();
            _msg.Payload = "tEST";
            // Enqueue the message
            _queueObj.EnqueueOptions.Visibility = OracleAQVisibilityMode.OnCommit;
            _queueObj.Enqueue(_msg);

            _txn.Commit();
            _queueObj.Dispose();
            _connObj.Close();
            _connObj.Dispose();
            _connObj = null;

        }
        public void OracleDEMQ()
        {
            string _connstring = "Data Source=intex1;User Id=intex;Password=intex;";
            OracleConnection _connObj = new OracleConnection(_connstring);

            // Create a new queue object
            OracleAQQueue _queueObj = new OracleAQQueue("UDT_NAME", _connObj);

            // Set the payload type to your UDT
            _queueObj.MessageType = OracleAQMessageType.Raw;
            _connObj.Open();
            OracleTransaction _txn = _connObj.BeginTransaction();
            // Dequeue the message.
            _queueObj.DequeueOptions.Visibility = OracleAQVisibilityMode.OnCommit;
            _queueObj.DequeueOptions.Wait = 10;
            OracleAQMessage _deqMsg = _queueObj.Dequeue();
            _txn.Commit();
            _queueObj.Dispose();
            _connObj.Close();
            _connObj.Dispose();
            _connObj = null;


        }

        /*
        public void OracleService()
        {
            while (true)
            { 
                try
                {
                    ProcessMessage(queueName);
                }
                catch (Exception e)
                {
                    LogManager.WriteLog(201, e.Message);
                    GC.Collect();
                }

                //Sleep
                if (mre.WaitOne(cf.TIME_OUT, false))
                    break;
 
        }

        }
        private static void ProcessMessage(String queueName)
        {
            //Open database
            String strConn = "";
            OracleConnection oc = new OracleConnection(strConn);
            oc.Open();
     
     //连接MOM....(略)
            
            do
            {
              //读取MOM消息数据...(略)
                    try
                    {
                        //保存MOM消息数据到数据库的操作...(略)
                        
                        str = msgInfo.MsgName + " save to database success.";
                        LogManager.WriteLog(301, str);
                    }
                    catch (OracleException e) 
                    {
                        //In case of:   ORA-00028(session kill)
                        //              ORA-02396(exceed idle time)
                        //            ORA-01012(not logon)
                        //            ORA-12535(timeout)
                        if (e.Code == 2396 || e.Code == 1012 || e.Code == 28 || e.Code == 12535)
                        {
                            str = msgInfo.MsgName + " save to database fail, error code: " + e.Code.ToString() +", send message back and re-connect oracle.";
                            LogManager.WriteLog(301, str);

                            break; //quit loops
                        }
                        else //For other reason, just save data to dump file
                        {
                           //保存MOM消息数据到文件...(略)
                        }
                    }

                    catch (Exception e)
                    {
                        //保存MOM消息数据到文件...(略)
                    }
                }

             } while (true);

            oc.Close();
        }*/

    }
}


